package com.slipper.system.ai.controller;

import com.slipper.core.gson.GsonUtil;
import com.slipper.system.ai.bo.AiSupplierModelBO;
import com.slipper.system.ai.service.AiSupplierModelService;
import com.slipper.system.ai.vo.AiSupplierVO;
import com.slipper.util.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.ai.bo.AiSupplierBO;
import com.slipper.system.ai.service.AiSupplierService;
import com.slipper.constants.Constants;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/aiSupplier")
public class AiSupplierController {
    @Resource
    private AiSupplierService aiSupplierService;
    @Resource
    private AiSupplierModelService aiSupplierModelService;
    
    @PostMapping(value = "/queryBy")
    public String queryBy(@RequestBody AiSupplierBO bo) {
        if(Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("数据获取失败！");
        }
        return ResultUtil.successResult(aiSupplierService.queryBy(bo.getId()));
    }

    @PostMapping(value = "/queryList")
    public String queryList(@RequestBody AiSupplierBO bo) {
        List<AiSupplierBO> supplierList = aiSupplierService.queryList(bo);
        supplierList.forEach(supplier -> {
            AiSupplierModelBO qo = new AiSupplierModelBO();
            qo.setSupplierId(supplier.getId());
            supplier.setTypes(aiSupplierModelService.queryList(qo)
                    .stream()
                    .map(AiSupplierModelBO::getType)
                    .collect(Collectors.joining(",")));
        });
        return ResultUtil.successResult(supplierList);
    }
    
    @PostMapping(value = "/queryPage")
    public String queryPage(@RequestBody AiSupplierBO bo) {
        return ResultUtil.successResult(aiSupplierService.queryPage(bo));
    }

    @PostMapping(value = "/save")
    public String save(@RequestBody AiSupplierBO bo) {
        bo.setOperateId(Constants.getCurrentUser().getEmployeeId());
        return ResultUtil.saveResult(aiSupplierService.save(bo), bo);
    }

    @PostMapping(value = "/delete")
    public String delete(@RequestBody AiSupplierBO bo) {
        int result = aiSupplierService.delete(bo);
        result += aiSupplierModelService.deleteBySupplierId(bo.getId());
        return ResultUtil.deleteResult(result);
    }

    @PostMapping(value = "/exportData")
    public void exportData(HttpServletResponse response, @RequestBody AiSupplierBO bo) {
        AiSupplierVO vo = new AiSupplierVO();
        vo.setAiSupplierList(aiSupplierService.queryList(bo));
        vo.getAiSupplierList().forEach(aiSupplierBO -> {
            AiSupplierModelBO aiSupplierModelQO = new AiSupplierModelBO();
            aiSupplierModelQO.setSupplierId(aiSupplierBO.getId());
            vo.addAiSupplierModelList(aiSupplierModelService.queryList(aiSupplierModelQO));
        });
        String fileName = "supplier-" + DateUtil.format(new Date(), "dd-HH-mm-ss") + ".dat";
        FileUtil.downloadContent(response, fileName, AESUtil.encrypt(GsonUtil.toJson(vo)));
    }

    @PostMapping(value = "/importData")
    public String importData(AiSupplierBO bo) {
        if (bo.getFile() == null) {
            return ResultUtil.failureResult("请选择上传文件！");
        }
        try {
            String content = new String(bo.getFile().getBytes(), StandardCharsets.UTF_8);
            AiSupplierVO vo = GsonUtil.fromJson(AESUtil.decrypt(content), AiSupplierVO.class);
            aiSupplierService.saveList(vo.getAiSupplierList());
            aiSupplierModelService.saveList(vo.getAiSupplierModelList());
            return ResultUtil.successResult("导入供应商成功。");
        } catch (Exception e) {
            return ResultUtil.failureResult("文件解释出错！");
        }
    }
}
